<template>
  <div>
    <a-card style="margin-top: 24px" :bordered="false" title="部门管理列表">
      <div
        style="float: left;width:35%;margin-top: 30px;border: 1px solid #dfdfdf;padding: 15px;margin-left: 25px;min-height: 600px"
      >
        <!-- <a-tree
          :props="props"
          :load="loadNode"
          :accordion="true"
          default-expand-all
          @node-click="handleNodeClick"
          lazy
        ></a-tree>-->
        <a-tree :load-data="onLoadData" :tree-data="treeData" />
      </div>
      <div
        style="float: left;width:60%;border: 1px solid #dfdfdf;margin-top: 30px;padding: 15px;margin-left: 25px;min-height: 600px"
      >
        <template v-if="createDisabled">
          <div style="margin-left: 50px;">
            <a-button type="primary" @click="hideForm(1)" v-if="createFormDisabled">新建子公司</a-button>
            <!--
            <a-button type="primary" @click="hideForm(2)" v-if="updateFormDisabled">修改公司信息</a-button>
            -->
            <a-button type="danger" @click="remove()" v-if="removeFormDisabled">删除公司</a-button>
          </div>
          <!-- <ou-form ref="ouForm" :formItem="formItem" :rules="rules" :dataInfo="dataInfo">
            <template slot="slotButton">
              <a-button type="primary" @click="create()">提交</a-button>
              <a-button type="info" @click="reset">重置</a-button>
            </template>
          </ou-form>-->
        </template>
      </div>
    </a-card>
  </div>
</template>
<script>
import axios from 'axios'
import { httpAction } from '@/api/manage'
export default {
  data() {
    return {
      treeData: [
        { title: 'Expand to load', key: '0' },
        { title: 'Expand to load', key: '1' },
        { title: 'Tree Node', key: '2', isLeaf: true },
      ],

      url: {
        listFist: '/online/user/getOwnerPartyId',
        listCompany: '/online/user/listSysCompany',
        selectAllCompanyTree: '/online/user/getOwnerPartyId',
      },

      parentId: '',
      editType: 1,
      createDisabled: false,
      createFormDisabled: false,
      updateFormDisabled: false,
      removeFormDisabled: false,
      props: {
        label: 'name',
        value: 'id',
        childCount: 'childCount',
        children: 'children',
        isLeaf: 'isLeaf',
      },
    }
  },
  methods: {
    getFirstNode() {
      let data = []
      let that = this
      let httpurl = this.url.listCompany
      let dataInfo = {parentId: '_NA_'}
      httpAction(httpurl, dataInfo).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          data = res.result
          this.treeData = res.result;
        } else {
          that.$message.warning(res.message)
        }
      })
      return data
    },
    getOtherNode() {
      let data = []
      let that = this
      let httpurl = this.url.selectAllCompanyTree
      let dataInfo = {}
      httpAction(httpurl, dataInfo).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          data = res.result
        } else {
          that.$message.warning(res.message)
        }
      })
      return data
    },

    onLoadData(treeNode) {
      return new Promise((resolve) => {
        if (treeNode.dataRef.children) {
          resolve()
          return
        }
        setTimeout(() => {
          treeNode.dataRef.children = [
            { title: 'Child Node', key: `${treeNode.eventKey}-0` },
            { title: 'Child Node', key: `${treeNode.eventKey}-1` },
          ]
          this.treeData = [...this.treeData]
          resolve()
        }, 1000)
      })
    },


    async loadNode(node, resolve) {
      if (node.level === 0) {
        let data = await this.getFirstNode()
        //查询本级基本信息
        return resolve([data])
      } else if (node.level > 0) {
        this.handleNodeClick(node.data, node)
        console.log(node.data.id)
        let data = await this.getOtherNode(node.data.id)
        return resolve(data)
      }
    },
    async handleNodeClick(data, node) {
      let parentId = data.id
      this.parentId = parentId
      this.editType = 2
      if (node.level <= 1) {
        //新建公司
        this.createDisabled = true
        this.createFormDisabled = true
        this.updateFormDisabled = true
        this.removeFormDisabled = false
        const res = await axios.post(this.$api.getCoCompany, { partyId: this.parentId })
        this.dataInfo = res.body
      } else {
        //修改,删除公司
        this.createDisabled = true
        this.createFormDisabled = true
        this.updateFormDisabled = true
        this.removeFormDisabled = true
        const res = await axios.post(this.$api.getCoCompany, { partyId: this.parentId })
        this.dataInfo = res.body
      }
    },
    // async getFirstNode() {
    //   const res = await axios.post(this.$api.getCoPartyGroupLazyFirst, {})
    //   return res.body
    // },
    // async getOtherNode(id) {
    //   const res = await axios.post(this.$api.getCoPartyGroupLazy, { root: id })
    //   return res.body
    // },
    async hideForm(level) {
      this.editType = level
      if (level == 2) {
        const res = await axios.post(this.$api.getCoCompany, { partyId: this.parentId })
        this.dataInfo = res.body
      } else {
        this.dataInfo = {}
        this.reset()
      }
    },
    create() {
      if (this.editType == '1') {
        this.dataInfo.parentId = this.parentId
        this.createDataInfoExt(this.$api.createCoCompany, this.dataInfo, 'FindCompanyCo')
        this.reload()
      } else {
        delete this.dataInfo['parentId']
        this.updateDataInfoExt(this.$api.updateCoCompany, this.dataInfo, 'FindCompanyCo')
      }
    },
    async remove() {
      this.dataInfo = {}
      this.dataInfo.partyId = this.parentId
      console.log(this.dataInfo)
      this.$confirm('是否确定删除,一旦删除不可恢复？', '警告', { type: 'error' }).then((mes) => {
        this.$http.post(this.$api.removeCoCompany, this.dataInfo).then((res) => {
          if (res.code === '200') {
            this.successMessage('删除成功')
            setTimeout(() => {
              this.reload()
            }, 500)
          }
        })
      })
    },
  },
  created() {
    this.getFirstNode()

    // for (let key in this.dataParams) {
    //   if (!this.params[key]) {
    //     this.params[key] = this.dataParams[key]
    //   }
    // }
    // this.loading = false
  },
}
</script>
